package 设计模式.策略模式;

import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author sky
 */
public class Context {
    private StrategyEnum strategy;
    private static final Map<StrategyEnum, Context> cache = new ConcurrentHashMap<>(4);

    private Context(StrategyEnum strategy) {
        this.strategy = strategy;
    }

    public int getResult(int x, int y) {
        return strategy.get().apply(x, y);
    }

    public static Context of(StrategyEnum strategy){
        Objects.requireNonNull(strategy,"parameter is not null");
        return  cache.computeIfAbsent(strategy,Context::new);
    }
}
